package org.mybatis.generator.api.plus;

import java.io.*;

/**
 * @author wangfupeng
 */
public class MybatisUtilResponse {

    public static void writeSimpleResponse(String basePackageName, String targetProject) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = basePackageName.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/controller/response");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/controller/response/SimpleResponse.java");
            //如果文件不存在，则创建文件
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + basePackageName + ".controller.response;\n");
            bufferedWriter.write("\n" +
                    "import io.swagger.annotations.ApiModel;\n" +
                    "import io.swagger.annotations.ApiModelProperty;\n" +
                    "\n" +
                    "import java.io.Serializable;\n" +
                    "\n" +
                    "/**\n" +
                    " * @author wangfupeng\n" +
                    " */\n" +
                    "@ApiModel(\"单值返回\")\n" +
                    "public class SimpleResponse<T> implements Serializable {\n" +
                    "\n" +
                    "    private static final long serialVersionUID = 1L;\n" +
                    "    protected static final int SUCCESS_STATUS = 200;\n" +
                    "    protected static final String SUCCESS_MESSAGE = \"success\";\n" +
                    "\n" +
                    "    private T data;\n" +
                    "    \n" +
                    "    @ApiModelProperty(value = \"返回消息,成功为success\", example = \"success\")\n" +
                    "    private String message;\n" +
                    "    \n" +
                    "    @ApiModelProperty(value = \"返回状态,成功为200\", example = \"200\")\n" +
                    "    private int status;\n" +
                    "    \n" +
                    "    public T getData() {\n" +
                    "        return data;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setData(T data) {\n" +
                    "        this.data = data;\n" +
                    "    }\n" +
                    "\n" +
                    "    public String getMessage() {\n" +
                    "        return message;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setMessage(String message) {\n" +
                    "        this.message = message;\n" +
                    "    }\n" +
                    "\n" +
                    "    public int getStatus() {\n" +
                    "        return status;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setStatus(int status) {\n" +
                    "        this.status = status;\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> success() {        return success((T) \"\");    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> success(T data) {\n" +
                    "        return success(data, SUCCESS_MESSAGE, \"\");\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> success(T data, String message, String moreInfo) {\n" +
                    "        return success(data, message, SUCCESS_STATUS, moreInfo);\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> success(T data, String message, int responseStatus, String moreInfo) {\n" +
                    "        SimpleResponse response = new SimpleResponse();\n" +
                    "        response.setData(data);\n" +
                    "        response.setStatus(responseStatus);\n" +
                    "        response.setMessage(message);\n" +
                    "        return response;\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> error(int responseStatus, String message, String moreInfo) {\n" +
                    "        SimpleResponse response = new SimpleResponse();\n" +
                    "        response.setStatus(responseStatus);\n" +
                    "        response.setMessage(message);\n" +
                    "        return response;\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> error(int responseStatus, String message) {\n" +
                    "        return error(responseStatus, message, null);\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> SimpleResponse<T> error(int responseStatus) {\n" +
                    "        return error(responseStatus, null, null);\n" +
                    "    }\n" +
                    "}\n\n");
            bufferedWriter.write("\n");
            System.out.println("write to file success : "+file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeListResponse(String basePackageName, String targetProject) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = basePackageName.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/controller/response");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/controller/response/ListResponse.java");
            //如果文件不存在，则创建文件
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + basePackageName + ".controller.response;\n");
            bufferedWriter.write("\n");
            bufferedWriter.write("\n" +
                    "import io.swagger.annotations.ApiModel;\n" +
                    "import io.swagger.annotations.ApiModelProperty;\n" +
                    "\n" +
                    "import java.util.List;\n" +
                    "\n" +
                    //"/** @author wangfupeng */\n" +
                    "@ApiModel(\"列表返回\")\n" +
                    "public class ListResponse<T> extends SimpleResponse {\n" +
                    "\n" +
                    "    public static final int DEFAULT_PAGE_SIZE = 50;\n" +
                    "\n" +
                    "    @ApiModelProperty(value = \"总数\", example = \"1\")\n" +
                    "    private Integer total; // 总数\n" +
                    "\n" +
                    "    @ApiModelProperty(value = \"页大小\", example = \"20\")\n" +
                    "    private Integer pageSize; // 页大小\n" +
                    "\n" +
                    "    @ApiModelProperty(value = \"总页数\", example = \"1\")\n" +
                    "    private Integer totalPage; // 总页数\n" +
                    "\n" +
                    "    @ApiModelProperty(value = \"当前页\", example = \"1\")\n" +
                    "    private Integer page; // 当前页\n" +
                    "\n" +
                    "    @ApiModelProperty(value = \"当前偏移量\", example = \"20\")\n" +
                    "    private Integer offset; // 当前偏移量\n" +
                    "\n" +
                    "    public Integer getTotal() {\n" +
                    "        return total;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setTotal(Integer total) {\n" +
                    "        this.total = total;\n" +
                    "    }\n" +
                    "\n" +
                    "    public Integer getPageSize() {\n" +
                    "        return pageSize;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setPageSize(Integer pageSize) {\n" +
                    "        this.pageSize = pageSize;\n" +
                    "    }\n" +
                    "\n" +
                    "    public Integer getTotalPage() {\n" +
                    "        return totalPage;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setTotalPage(Integer totalPage) {\n" +
                    "        this.totalPage = totalPage;\n" +
                    "    }\n" +
                    "\n" +
                    "    public Integer getPage() {\n" +
                    "        return page;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setPage(Integer page) {\n" +
                    "        this.page = page;\n" +
                    "    }\n" +
                    "\n" +
                    "    public Integer getOffset() {\n" +
                    "        return offset;\n" +
                    "    }\n" +
                    "\n" +
                    "    public void setOffset(Integer offset) {\n" +
                    "        this.offset = offset;\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> ListResponse<T> success(List<T> data, int totalCount, int page) {\n" +
                    "        return success(data, totalCount, page, 20);\n" +
                    "    }\n" +
                    "\n" +
                    "    public static <T> ListResponse<T> success(List<T> data, int totalCount, int page, int pageSize) {\n" +
                    "        ListResponse response = new ListResponse();\n" +
                    "        response.setData(data);\n" +
                    "        response.setStatus(SUCCESS_STATUS);\n" +
                    "        response.setMessage(SUCCESS_MESSAGE);\n" +
                    "\n" +
                    "        if (pageSize == 0)\n" +
                    "            pageSize = DEFAULT_PAGE_SIZE;\n" +
                    "\n" +
                    "        int totalPage = (int) Math.ceil((double) totalCount / pageSize);\n" +
                    "\n" +
                    "        if (page > totalPage)\n" +
                    "            page = totalPage;\n" +
                    "        if (page <= 0)\n" +
                    "            page = 1;\n" +
                    "\n" +
                    "        int offset = pageSize * (page - 1);\n" +
                    "        response.setTotal(totalCount);\n" +
                    "        response.setPageSize(pageSize);\n" +
                    "        response.setTotalPage(totalPage);\n" +
                    "        response.setPage(page);\n" +
                    "        response.setOffset(offset);\n" +
                    "        return response;\n" +
                    "    }\n" +
                    "\n" +
                    "}\n");
            bufferedWriter.write("\n");
            bufferedWriter.write("\n");
            bufferedWriter.write("\n");
            bufferedWriter.write("\n");
            System.out.println("write to file success : "+file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
